/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evolve.webbot.file;
import  java.io.File;
import  java.util.Date;
import  java.text.SimpleDateFormat;
import  java.util.ArrayList;
import  java.io.FileWriter;
import  java.io.BufferedWriter;
import  java.io.PrintWriter;
import  java.io.BufferedReader;
import  java.io.IOException;

import  com.evolve.webbot.bean.Coppel;
import java.io.FileReader;
import java.util.StringTokenizer;

/**
 *
 * @author DarthAE
 */
public class CommonDataFile 
{
    
    public CommonDataFile(){}
    
    public void writeCommonWebTxtFile(String path,String content)
    {
              PrintWriter pw   = null;
       
       System.out.println(path);
                   try{     
                       File file = new File(path);
                                             
                       if(!file.exists())
                       {
                           file.createNewFile();
                       }
                       
                       FileWriter  fw    = new FileWriter(file);  
                       BufferedWriter bw = new BufferedWriter(fw);
                       
                       bw.write(content+"\n");
                       bw.close();
                        
                      } catch (Exception e) {e.printStackTrace();}       
    }
    
    public void writeCommonWebTxtFile(String path,
                                      ArrayList<ArrayList<String>> contentList)
    {
       if(path.contains(".xls"))
       {
          path=path.replace(".xls",".txt");
       }
       
       PrintWriter pw   = null;
       
                   try{     
                       File file = new File(path);
                       FileWriter fw    = new FileWriter(file.getAbsoluteFile()); 
                       BufferedWriter bw = new BufferedWriter(fw);
                       
                        pw      = new PrintWriter(file);
                        
                        for(int i=0; i<contentList.size();i++)
                        {
                            bw.write(contentList.get(i).toString());
                        }                         
                        
                        bw.close();
                        
                      } catch (Exception e) {e.printStackTrace();}       
    }  
    
    public void writeCommonWebTxtFileCoppel(String path,Date date,
                                            ArrayList<Coppel> contentList)
    {
       String header="";
       CoppelDataFile coppelDataFile = new CoppelDataFile();
       
       header = coppelDataFile.createHeaderFileCoppel(date);
       
       if(path.contains(".xls"))
       {
          path=path.replace(".xls",".txt");
          path=path.replace("vyc","data");
       }
       
       FileWriter  file = null;
       PrintWriter pw   = null;
       
                   try{                      
                        file    = new FileWriter(path);                     
                        pw      = new PrintWriter(file);
                        
                        pw.println(header);
                        
                        for(int i=0; i<contentList.size();i++)
                        {
                            pw.println(contentList.get(i).toString());
                        }                         
                        
                      } catch (Exception e) {e.printStackTrace();}
                        finally {
                                  try {           
                                        if (null != file)
                                        {
                                            file.close();
                                        }
                                       } catch (Exception e2) {e2.printStackTrace();}
                                }        
    }
    
    public void writeCommonWebTxtFileRetail(String path,
                                            ArrayList<String> contentList)
    {

          path=path.replace("vyc","data");

       
       FileWriter  file = null;
       PrintWriter pw   = null;
       
                   try{                      
                        file    = new FileWriter(path);                     
                        pw      = new PrintWriter(file);
                        
                        for(int i=0; i<contentList.size();i++)
                        {
                            pw.println(contentList.get(i).toString());
                        }                         
                        
                      } catch (Exception e) {e.printStackTrace();}
                        finally {
                                  try {           
                                        if (null != file)
                                        {
                                            file.close();
                                        }
                                       } catch (Exception e2) {e2.printStackTrace();}
                                }        
    }  
    
    public void writeCommonWebTxtFileRetail(String path,
                                            ArrayList<String> contentList,int index,String fecha)
    {
       Date date = new Date();
       path=path.replace("vyc","data");
       SimpleDateFormat sdf00 = new SimpleDateFormat("yyyyMM");
       SimpleDateFormat sdf01 = new SimpleDateFormat("dd");
       int    dia     = 0;
       String strDia  = sdf01.format(date);
       String strDate = sdf00.format(date);
       
       dia = Integer.parseInt(strDia) - index;
       
       if(String.valueOf(dia).length()==1)
       {
           strDia = "0"+dia;
       }
       else
       {
           strDia = String.valueOf(dia);
       }
       
       String header ="store_type_desc|store_name|store_nbr|upc|pos_qty_"+fecha+"|"+
                      "pos_sales_"+fecha;
       
       
       FileWriter  file = null;
       PrintWriter pw   = null;
       
                   try{                      
                        file    = new FileWriter(path);                     
                        pw      = new PrintWriter(file);
                        
                        //pw.println(header);
                        for(int i=0; i<contentList.size();i++)
                        {
                            pw.println(contentList.get(i).toString());
                        }                         
                        
                      } catch (Exception e) {e.printStackTrace();}
                        finally {
                                  try {           
                                        if (null != file)
                                        {
                                            file.close();
                                        }
                                       } catch (Exception e2) {e2.printStackTrace();}
                                }        
    }     
    
    public  ArrayList<String> readLiverpoolRetail(String path)
    {
      ArrayList<String> list = new ArrayList<String>();
      StringTokenizer tokens = null;
            
      try {
             BufferedReader br = new BufferedReader(new FileReader(path));
             String linea = br.readLine();

             while (linea != null) 
             {

               linea = linea.replace("\t","|");
               /*System.out.println(linea);*/
               list.add(linea);
               linea = br.readLine();
             }
             
           } catch(IOException e){}
      
      return list;
    }
    
    public  ArrayList<String> readSoriana(String path)
    {
      ArrayList<String> list = new ArrayList<String>();
      StringTokenizer tokens = null;
            
      try {
             BufferedReader br = new BufferedReader(new FileReader(path));
             String linea = br.readLine();

             while (linea != null) 
             {

               linea = linea.replace("!","|");
               list.add(linea);
               linea = br.readLine();
             }
             
           } catch(IOException e){}
      
      return list;
    }    
    
    public String getHeaderCoppel()
    {
        String header   = "";
        String strMonth = "";
        int    month  =0;
        Date date = new Date();
        
        SimpleDateFormat sdf00 = new SimpleDateFormat("MM");
        month = Integer.parseInt(sdf00.format(date));
        
        for(int i=(month-1);i>((month-1)-3);i--)
        {            
            if(String.valueOf(i).length()==1)
            {
                strMonth="0"+i;
            }
            else
            {
                strMonth=String.valueOf(i);
            }
            
            header=header+"|periodo_"+strMonth;
        }
        
        
        
        return header;
    }
    
    public static void main(String args[])
    {
        CommonDataFile cm = new CommonDataFile();
        
        System.out.println(cm.getHeaderCoppel());
    }
}
